﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;

namespace AdminBase.Common.Attributes
{
    /// <summary>
    /// 日志
    /// </summary>
    public class LogActionFilterAttribute : Attribute, IActionFilter
    {
        // 变量实例
        private readonly ILogger<LogActionFilterAttribute> _logger;

        /// <summary>
        /// 构造函数注册
        /// </summary>
        /// <param name="logger"></param>
        public LogActionFilterAttribute(ILogger<LogActionFilterAttribute> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 在方法执行之前
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var controllerName = context.RouteData.Values["controller"];
            var actionName = context.RouteData.Values["action"];
            _logger.LogInformation($"开始执行{controllerName}/{actionName}");
        }

        /// <summary>
        /// 在方法执行之后
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var controllerName = context.RouteData.Values["controller"];
            var actionName = context.RouteData.Values["action"];
            _logger.LogInformation($"执行完成：{controllerName}/{actionName}");
        }

    }
}
